//
//  CMLOEUnitTest.m
//  CocoaMesh
//
//  Created by George  Armah on 5/21/09.
//  Copyright 2009 Lafayette College. All rights reserved.
//

#import "GTMSenTestCase.h"
#import "CMTestLOE.h"

@interface CMLOEUnitTest : SenTestCase
{
	
}

-(void)testConnection;
-(void)testloadMeshObjects;
-(void)testupdateResource;
-(void)testdeleteResource;
-(void)testcreateMeshObject;
-(BOOL)checkMeshResource:(CMResource*)resource;

@end


@implementation CMLOEUnitTest

-(BOOL)checkMeshResource:(CMResource*)resource
{
	BOOL resourceIsGood = YES;
	NSArray * keysToCheck = [NSArray arrayWithObjects:CMBASEURIKEY, CMTITLEKEY, CMSELFLINKKEY,
							 CMCATEGORIESKEY, nil];
	for ( NSString * key in keysToCheck )
	{
		resourceIsGood = resourceIsGood && ([resource valueForKey:key] != nil);
		if (!resourceIsGood)
		{
			NSLog(@"\n Resource %@ doesn't contain value for key \n\n %@", [resource description], key);
			return resourceIsGood;
		}
	}
	return resourceIsGood;
}







#pragma mark Test Cases
-(void)testConnection
{

	CMLOE * loe = [CMTestLOE sharedCMLOE];
	NSString * u = [CMTestLOE objectfromPliist:CMTESTDATA withKey:CMTESTUSERNAME];
	NSString * p = [CMTestLOE objectfromPliist:CMTESTDATA withKey:CMTESTPASSWORD];
	NSError * e = nil;
	BOOL conn = [loe connectWithUserID:u password:p applicationID:@"" connectionError:&e];
	STAssertTrue(conn, @"\n\n Unable to connect to MOE \n %@ \n %@ \n %@ \n\n", u, p, [e description]);
	
}


-(void)testloadMeshObjects
{
	//load meshobjects ... check that each dude in array is a mesh object
	CMLOE * loe = [CMTestLOE sharedCMLOE];
	NSArray * meshObjects = [loe loadMeshObjects:nil];
	STAssertNotNil(meshObjects, @"Mesh object array is nil: %@", meshObjects);
	
	
	for (CMMeshObject * m in meshObjects)
	{
		STAssertTrue( [m isMemberOfClass:[CMMeshObject class]], @"Mesh object %@ is not a member of class CMMeshObject", m);
		STAssertTrue([self checkMeshResource:m], @"Bad mesh object %@ encountered", m);
	}
}



-(void)testupdateResource
{
	//load meshobjects, pick one random one, change a key, update, load again and find it by key
	//check that updating persisted
	CMLOE * loe = [CMTestLOE sharedCMLOE];
	NSArray * meshObjects = [loe loadMeshObjects:nil];
	//NSLog(@"\n\n%@\n\n", meshObjects);
	CMMeshObject * firstM = [meshObjects objectAtIndex:0];
	
	NSString * newTitle = [NSString stringWithFormat:@"Test Title %@", [[NSDate date] description]];
	[firstM setValue:newTitle forKey:CMTITLEKEY];
	
	[loe updateResource:firstM error:nil];
	
	//NSLog(@"\n\n\n");
	CMMeshObject * updatedM = [loe loadMeshObjectWithKey:CMTITLEKEY forValue:[NSString stringWithFormat:@"\'%@\'", newTitle] error:nil];
	//NSLog(@"\n\n\n");
	
	STAssertTrue([[updatedM valueForKey:CMTITLEKEY] isEqualToString:newTitle], @"Title of updated Mesh resource %@ is %@ instead of %@", updatedM, [updatedM valueForKey:CMTITLEKEY], newTitle);
	
}



-(void)testdeleteResource
{
	//create a mesh resource, load mesh resources, check that it was created,
	//delete it , load mesh objects, check that it was deleted
}

-(void)testcreateMeshObject
{
	//create a mesh object, load meshobjects, check that it was created
}

@end
